pascal问题: ax^2(2次方)+bx+c=0,求X的值?

来源:百度知道 编辑:UC知道 时间:2024/05/21 09:13:27
我是pascal初初初初学者,请问 ax^2(2次方)+bx+c=0,求X的值?这题是否是这样做的?如不是,请修正或附正确答案!谢谢!
program program;
var
d,a,b,c:real;
begin
readln(a,b,c);
d:=(sqr(b)-4*a*c);
if d>0 then writeln((-b+sqrt(d))/(2*a):10:2,(-b-d)/(2*a):10:2);
if d=0 then writeln((-b+sqrt(d))/(2*a):10:2);
if d<0 then writeln('There is no root!');
end.
以上为本人所写,觉得不怎么正确,请多多指教!

这是一元二次方程嘛。
program roots;
var
a,b,c,delta,x1,x2:real;
begin
readln(a,b,c);
if a<>0 then
begin
delta:=b*b-4*a*c;
if delta>=0 then
begin
x1:=(-b+sqrt(delta))/(2*a);
x2:=(-b-sqrt(delta))/(2*a);
writeln('x=',x1,'or',x2);
end;
else writeln('No real roots!');
end;
10:
end.

program program1; {怎么能关键字做标识符?}
var
d,a,b,c:real;
begin
readln(a,b,c);
if a = 0 then {漏了细节}
begin
if (b = 0)and(c = 0) then writeln('Any real suit x !')
else if (b=0) then
writeln('There is no root') else
writeln(-c/b:10:2);
halt;
end;
d:=sqr(b)-4*a*c;
if d>0 then writeln((-b+sqrt(d))/(2*a):10:2,(-b-sqrt(d))/(2*a):10:2); {这里有个小错误}
if d=0 then writeln(-b/(2*a):10:2); {简化一些}
if d<0 then writeln('There is no root!');
end.